package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 1302. 层数最深叶子节点的和
 * @author feizns
 * @since 2020/3/17
 */
public class DeepestLeavesSum {

    public static void main(String[] args) {
        System.out.println(deepestLeavesSum(create()));
    }

    private static TreeNode create() {
        TreeNode ret = new TreeNode(1);
        return ret;
    }

    public static int deepestLeavesSum(TreeNode root) {
        int sum = 0;
        if ( root != null ) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while ( !queue.isEmpty() ) {
                sum = 0;
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.remove();
                    sum += node.val;
                    if ( node.left != null )
                        queue.add(node.left);
                    if ( node.right != null )
                        queue.add(node.right);
                }
            }
        }
        return sum;
    }

}
